package n1330;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	
	static boolean flag[]=new boolean[11001];
	
	static int parent[]=new int[11001];
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

		Scanner cin =new Scanner(bf);
		
		int t=cin.nextInt();
		for(int i=0;i<t;i++)
		{
			Arrays.fill(flag, false);
			Arrays.fill(parent, -1);
			int n=cin.nextInt();
			for(int j=0;j<n-1;j++)
			{
				
				int p=cin.nextInt();
				int s=cin.nextInt();
				parent[s]=p;
			}
			int n1=cin.nextInt();
			int n2=cin.nextInt();
			while(n1!=-1)
			{
				flag[n1]=true;
				n1=parent[n1];
			}
			while(n2!=-1)
			{
				if(flag[n2]){
					System.out.println(n2);
					break;
				}
				n2=parent[n2];
			}
		}
	}

}
